package migration

import (
	"api/model"
	"database/sql"
	"migrate/app/migrate"
	"migrate/helper"

	"gorm.io/gorm"
)

func init() {
	migrate.AddMigration(&migrate.Migration{
		Version: "m_20220526085705_create_user",
		Up:      m_20220526085705_create_user_up,
		Down:    m_20220526085705_create_user_down,
	})
}

func m_20220526085705_create_user_up(db *gorm.DB) error {
	err := db.AutoMigrate(&model.User{})
	if err != nil {
		return err
	}

	salt := helper.GenerateRandomString(32)
	superadmin := model.User{
		Username: "superadmin",
		Email:    "superadmin@dropjar.com",
		Phone: sql.NullString{
			String: "",
			Valid:  true,
		},
		Role: sql.NullInt16{
			Int16: 0,
			Valid: true,
		}, // 0: superadmin 1: admin 100: user
		Password: helper.Md5("superadmin" + salt), // md5(password+salt) superadmin's default password is "superadmin"
		Salt:     salt,
		AppName:  "superadmin", // Value is Username when Role is 0/1
		AppKey:   "superadmin",
		Comment:  "",
		Enable: sql.NullInt16{
			Int16: 0,
			Valid: true,
		},
	}
	err = db.Create(&superadmin).Error
	if err != nil {
		return err
	}
	return nil
}

func m_20220526085705_create_user_down(db *gorm.DB) error {
	return nil
}
